import 'package:flutter/material.dart';
import 'package:sale/widgets/common_widgets.dart';
import 'package:sale/constants/app_constants.dart';
import 'package:sale/models/hotel.dart';

class HotelPage extends StatefulWidget {
  const HotelPage({super.key});

  @override
  State<HotelPage> createState() => _HotelPageState();
}

class _HotelPageState extends State<HotelPage>
    with SingleTickerProviderStateMixin {
  late AnimationController _controller;
  late Animation<double> _fadeAnimation;
  late Animation<Offset> _slideAnimation;

  // 当前选中的产品包
  HotelPackage? _selectedPackage;

  // 产品包列表
  late List<HotelPackage> _packages = [];
  late List<HotelServices> _services = [];

  @override
  void initState() {
    super.initState();

    // 初始化动画控制器
    _controller = AnimationController(
      duration: const Duration(milliseconds: 800),
      vsync: this,
    );

    _fadeAnimation = Tween<double>(
      begin: 0.0,
      end: 1.0,
    ).animate(CurvedAnimation(
      parent: _controller,
      curve: const Interval(0.0, 0.6, curve: Curves.easeOut),
    ));

    _slideAnimation = Tween<Offset>(
      begin: const Offset(0.0, 0.1),
      end: Offset.zero,
    ).animate(CurvedAnimation(
      parent: _controller,
      curve: const Interval(0.0, 0.6, curve: Curves.easeOutCubic),
    ));

    // 获取会议产品包数据
    try {
      _packages = getHotelPackages();
      _services = getHotelServices();
      // 默认选中第一个产品包
      if (_packages.isNotEmpty) {
        _selectedPackage = _packages.first;
      }
    } catch (e) {
      debugPrint('获取产品包数据失败: $e');
      _packages = [];
    }

    // 开始动画
    _controller.forward();
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
        child: GradientBackground(
          child: SlideTransition(
            position: _slideAnimation,
            child: FadeTransition(
              opacity: _fadeAnimation,
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.stretch,
                children: [
                  // 标题栏
                  _buildHeader(),
                  // 内容区域
                  Expanded(
                    child: _selectedPackage != null && _packages.isNotEmpty
                        ? Padding(
                            padding: const EdgeInsets.only(bottom: 8.0),
                            child: _buildPackageDetails(_selectedPackage!),
                          )
                        : Center(
                            child: Text('暂无数据',
                                style: AppConstants.subheadingStyle)),
                  ),
                ],
              ),
            ),
          ),
        ),
      ),
    );
  }

  // 构建页面标题
  Widget _buildHeader() {
    return Padding(
      padding: const EdgeInsets.all(16.0),
      child: Row(
        children: [
          // 返回按钮
          GestureDetector(
            onTap: () => Navigator.of(context).pop(),
            child: Container(
              padding: const EdgeInsets.all(12),
              decoration: BoxDecoration(
                color: Colors.white.withAlpha(51),
                borderRadius: BorderRadius.circular(14),
                border: Border.all(color: Colors.white.withAlpha(77)),
                boxShadow: [
                  BoxShadow(
                    color: Colors.black.withAlpha(20),
                    blurRadius: 8,
                    offset: const Offset(0, 2),
                  ),
                ],
              ),
              child:
                  const Icon(Icons.arrow_back, color: Colors.white, size: 20),
            ),
          ),
          const SizedBox(width: 16),
          // 标题
          Expanded(
            child: Container(
              padding: const EdgeInsets.symmetric(horizontal: 24, vertical: 12),
              decoration: BoxDecoration(
                gradient: LinearGradient(
                  colors: [
                    AppConstants.primaryColor.withAlpha(179),
                    AppConstants.secondaryColor.withAlpha(179),
                  ],
                  begin: Alignment.topLeft,
                  end: Alignment.bottomRight,
                ),
                borderRadius: BorderRadius.circular(20),
                boxShadow: [
                  BoxShadow(
                    color: AppConstants.primaryColor.withAlpha(100),
                    blurRadius: 12,
                    offset: const Offset(0, 4),
                  ),
                ],
              ),
              child: Text(
                '智慧酒店解决方案',
                style: AppConstants.headingStyle.copyWith(
                  fontSize: 24,
                  fontWeight: FontWeight.w900,
                  color: Colors.white,
                ),
                textAlign: TextAlign.center,
              ),
            ),
          ),
        ],
      ),
    );
  }

  // 构建产品包详情
  Widget _buildPackageDetails(HotelPackage package) {
    if (_packages.isEmpty) {
      return Center(
        child: Text('暂无产品包数据', style: AppConstants.subheadingStyle),
      );
    }

    return Padding(
      padding: const EdgeInsets.symmetric(horizontal: 20.0),
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          // 左侧产品包列表
          SizedBox(
            width: 250,
            child: _buildPackageList(),
          ),
          const SizedBox(width: 8),
          // 中间产品列表区域
          if (package.products.isNotEmpty)
            Expanded(
              flex: 4,
              child: _buildProductsList(package),
            )
          else
            Expanded(
              flex: 4,
              child: Container(
                padding: const EdgeInsets.all(24),
                decoration: BoxDecoration(
                  color: Colors.white.withAlpha(40),
                  borderRadius: BorderRadius.circular(24),
                  border: Border.all(
                      color: AppConstants.accentColor.withAlpha(70),
                      width: 1.5),
                  boxShadow: [
                    BoxShadow(
                      color: Colors.black.withAlpha(30),
                      blurRadius: 10,
                      offset: const Offset(0, 3),
                    ),
                  ],
                ),
                child: Center(
                  child: Text(
                    '该套餐暂无产品数据',
                    style: TextStyle(
                      fontSize: 18,
                      color: AppConstants.textColor,
                    ),
                  ),
                ),
              ),
            ),
          const SizedBox(width: 8),
          // 右侧产品包信息
          Expanded(
            flex: 2,
            child: _buildServicesList(),
          ),
        ],
      ),
    );
  }

  // 构建产品包列表
  Widget _buildPackageList() {
    final screenSize = MediaQuery.of(context).size;

    return Container(
      padding: const EdgeInsets.all(16),
      constraints: BoxConstraints(
        maxWidth: screenSize.width * 0.2,
        maxHeight: screenSize.height * 0.9,
      ),
      decoration: BoxDecoration(
        color: Colors.white.withAlpha(40),
        borderRadius: BorderRadius.circular(20),
        border: Border.all(color: Colors.white.withAlpha(80), width: 1.5),
        boxShadow: [
          BoxShadow(
            color: Colors.black.withAlpha(30),
            blurRadius: 10,
            offset: const Offset(0, 3),
          ),
        ],
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Row(
            children: [
              Container(
                padding: const EdgeInsets.all(8),
                decoration: BoxDecoration(
                  color: AppConstants.primaryColor.withAlpha(40),
                  borderRadius: BorderRadius.circular(8),
                ),
                child: Icon(
                  Icons.view_list_rounded,
                  color: AppConstants.primaryColor,
                  size: 20,
                ),
              ),
              const SizedBox(width: 8),
              Expanded(
                child: Text(
                  '产品套餐',
                  style: AppConstants.subheadingStyle.copyWith(
                    fontSize: 16,
                    fontWeight: FontWeight.bold,
                    color: AppConstants.textColor,
                  ),
                  maxLines: 1,
                  overflow: TextOverflow.ellipsis,
                ),
              ),
            ],
          ),
          Divider(
            height: 20,
            color: Colors.white.withAlpha(70),
            thickness: 1,
          ),
          // 产品包列表
          if (_packages.isEmpty)
            Center(
              child: Padding(
                padding: const EdgeInsets.all(16.0),
                child: Text(
                  '暂无产品包数据',
                  style: TextStyle(
                    fontSize: 14,
                    color: AppConstants.textColor,
                  ),
                ),
              ),
            )
          else
            Expanded(
              child: SingleChildScrollView(
                child: Column(
                  children: [
                    for (var i = 0; i < _packages.length; i++) ...[
                      _buildPackageListItem(_packages[i]),
                      if (i < _packages.length - 1) const SizedBox(height: 12),
                    ],
                  ],
                ),
              ),
            ),
        ],
      ),
    );
  }

  // 构建产品包列表项 - 使用id而不是name比较
  Widget _buildPackageListItem(HotelPackage package) {
    final isSelected = _selectedPackage?.id == package.id;

    return GestureDetector(
      onTap: () {
        setState(() {
          _selectedPackage = package;
        });
      },
      child: AnimatedContainer(
        duration: const Duration(milliseconds: 200),
        padding: const EdgeInsets.all(12),
        decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(14),
          color: isSelected
              ? AppConstants.primaryColor.withAlpha(40)
              : Colors.white.withAlpha(20),
          border: Border.all(
            color: isSelected
                ? AppConstants.primaryColor.withAlpha(150)
                : Colors.white.withAlpha(60),
            width: 1.5,
          ),
          boxShadow: isSelected
              ? [
                  BoxShadow(
                    color: AppConstants.primaryColor.withAlpha(40),
                    blurRadius: 8,
                    offset: const Offset(0, 2),
                  ),
                ]
              : null,
        ),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            // 产品包名称和标签
            Row(
              children: [
                Expanded(
                  child: Text(
                    package.name,
                    style: TextStyle(
                      fontSize: 15,
                      fontWeight: FontWeight.bold,
                      color: isSelected
                          ? AppConstants.primaryColor
                          : AppConstants.textColor,
                    ),
                    maxLines: 2,
                    overflow: TextOverflow.ellipsis,
                  ),
                ),
                if (package.tag.isNotEmpty) ...[
                  Container(
                    padding: const EdgeInsets.symmetric(
                      horizontal: 6,
                      vertical: 2,
                    ),
                    decoration: BoxDecoration(
                      color: _getTagColor(package.tag).withAlpha(40),
                      borderRadius: BorderRadius.circular(20),
                      border: Border.all(
                        color: _getTagColor(package.tag).withAlpha(100),
                        width: 1,
                      ),
                    ),
                    child: Text(
                      package.tag,
                      style: TextStyle(
                        fontSize: 11,
                        fontWeight: FontWeight.w500,
                        color: _getTagColor(package.tag),
                      ),
                    ),
                  ),
                ],
              ],
            ),
            const SizedBox(height: 8),
            // 价格
            Text(
              '¥${package.price.toStringAsFixed(2)}',
              style: TextStyle(
                fontSize: 17,
                fontWeight: FontWeight.w700,
                color: isSelected
                    ? AppConstants.accentColor
                    : AppConstants.textColor.withAlpha(179),
              ),
            ),
            const SizedBox(height: 6),
            // 描述
            Text(
              package.description,
              style: TextStyle(
                fontSize: 13,
                color: AppConstants.textColor.withAlpha(179),
              ),
              maxLines: 2,
              overflow: TextOverflow.ellipsis,
            ),
          ],
        ),
      ),
    );
  }

  // 根据标签获取颜色
  Color _getTagColor(String tag) {
    switch (tag) {
      case '热门':
      case '畅销':
        return Colors.orange;
      case '推荐':
      case '分期':
        return Colors.blue;
      case '高端':
      case '提升':
        return Colors.purple;
      default:
        return AppConstants.primaryColor;
    }
  }

  // 构建产品列表
  Widget _buildProductsList(HotelPackage package) {
    final screenSize = MediaQuery.of(context).size;

    return Container(
      padding: const EdgeInsets.all(20),
      constraints: BoxConstraints(
        maxWidth: screenSize.width * 0.2,
        maxHeight: screenSize.height * 0.9,
      ),
      decoration: BoxDecoration(
        color: Colors.white.withAlpha(40),
        borderRadius: BorderRadius.circular(24),
        border: Border.all(
            color: AppConstants.accentColor.withAlpha(70), width: 1.5),
        boxShadow: [
          BoxShadow(
            color: Colors.black.withAlpha(30),
            blurRadius: 10,
            offset: const Offset(0, 3),
          ),
        ],
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Row(
            children: [
              Container(
                padding: const EdgeInsets.all(8),
                decoration: BoxDecoration(
                  color: AppConstants.accentColor.withAlpha(40),
                  borderRadius: BorderRadius.circular(10),
                ),
                child: Icon(
                  Icons.inventory_2_rounded,
                  color: AppConstants.accentColor,
                  size: 22,
                ),
              ),
              const SizedBox(width: 10),
              Expanded(
                child: Text(
                  '包含产品',
                  style: AppConstants.subheadingStyle.copyWith(
                    fontSize: 18,
                    fontWeight: FontWeight.bold,
                    color: AppConstants.textColor,
                  ),
                  maxLines: 1,
                  overflow: TextOverflow.ellipsis,
                ),
              ),
              const SizedBox(width: 10),
              Container(
                padding:
                    const EdgeInsets.symmetric(horizontal: 10, vertical: 3),
                decoration: BoxDecoration(
                  color: AppConstants.primaryColor.withAlpha(40),
                  borderRadius: BorderRadius.circular(14),
                  border: Border.all(
                    color: AppConstants.primaryColor.withAlpha(100),
                    width: 1,
                  ),
                ),
                child: Text(
                  '共${package.products.length}件',
                  style: TextStyle(
                    fontSize: 14,
                    color: AppConstants.primaryColor,
                    fontWeight: FontWeight.w500,
                  ),
                ),
              ),
            ],
          ),
          Divider(
            height: 24,
            color: Colors.white.withAlpha(70),
            thickness: 1,
          ),
          // 产品列表
          if (package.products.isEmpty)
            Expanded(
              child: Center(
                child: Text(
                  '该套餐暂无产品',
                  style: TextStyle(
                    fontSize: 16,
                    color: AppConstants.textColor,
                  ),
                ),
              ),
            )
          else
            Expanded(
              child: ListView.separated(
                padding: const EdgeInsets.only(bottom: 16),
                itemCount: package.products.length,
                separatorBuilder: (context, index) => Divider(
                  height: 32,
                  color: Colors.white.withAlpha(50),
                  thickness: 0.5,
                ),
                itemBuilder: (context, index) {
                  if (index >= package.products.length) {
                    return SizedBox.shrink();
                  }
                  final product = package.products[index];
                  return _buildProductItem(product, index);
                },
              ),
            ),
        ],
      ),
    );
  }

  // 构建产品项
  Widget _buildProductItem(HotelProduct product, int index) {
    // 选择图标和颜色
    IconData productIcon;
    Color iconColor;

    switch (product.name) {
      case '酒店组网（包含）':
        productIcon = Icons.router_rounded;
        iconColor = Colors.blue.shade400;
        break;
      case '酒店应用（赠送）':
        productIcon = Icons.monitor_rounded;
        iconColor = Colors.teal.shade400;
        break;
      case '酒店增值服务':
        productIcon = Icons.tablet_rounded;
        iconColor = Colors.orange.shade400;
        break;
      default:
        productIcon = Icons.devices_rounded;
        iconColor = Colors.grey.shade700;
    }

    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        // 产品标题和价格
        Row(
          children: [
            // 产品图标
            Container(
              padding: const EdgeInsets.all(12),
              decoration: BoxDecoration(
                color: iconColor.withAlpha(40),
                borderRadius: BorderRadius.circular(14),
                boxShadow: [
                  BoxShadow(
                    color: iconColor.withAlpha(30),
                    blurRadius: 6,
                    offset: const Offset(0, 2),
                  ),
                ],
              ),
              child: Icon(
                productIcon,
                color: iconColor,
                size: 24,
              ),
            ),
            const SizedBox(width: 16),
            // 产品名称和销售品编码
            Expanded(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text(
                    product.name,
                    style: TextStyle(
                      fontSize: 18,
                      fontWeight: FontWeight.bold,
                      color: AppConstants.textColor,
                    ),
                    maxLines: 1,
                    overflow: TextOverflow.ellipsis,
                  ),
                ],
              ),
            ),
          ],
        ),
        const SizedBox(height: 16),
        // 产品内容
        Container(
          padding: const EdgeInsets.all(16),
          decoration: BoxDecoration(
            color: Colors.white.withAlpha(35),
            borderRadius: BorderRadius.circular(16),
            border: Border.all(
              color: iconColor.withAlpha(70),
              width: 1,
            ),
            boxShadow: [
              BoxShadow(
                color: Colors.black.withAlpha(20),
                blurRadius: 6,
                offset: const Offset(0, 2),
              ),
            ],
          ),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            mainAxisSize: MainAxisSize.min, // 修复: 确保内部Column不会溢出
            children: [
              // 产品内容列表
              if (product.content.isEmpty)
                Center(
                  child: Text(
                    '暂无产品内容',
                    style: TextStyle(
                      fontSize: 20,
                      color: AppConstants.textColor.withAlpha(179),
                    ),
                  ),
                )
              else
                ...product.content.map((item) => Padding(
                      padding: const EdgeInsets.all(8),
                      child: Row(
                        crossAxisAlignment: CrossAxisAlignment.center,
                        children: [
                          Icon(
                            Icons.check_circle_outline,
                            size: 20,
                            color: iconColor.withAlpha(210),
                          ),
                          const SizedBox(width: 8),
                          Expanded(
                            child: Text(
                              item,
                              style: TextStyle(
                                fontSize: 20, // 调整字体大小以避免溢出
                                color: AppConstants.textColor.withAlpha(230),
                                height: 1.4,
                              ),
                            ),
                          ),
                        ],
                      ),
                    )),
            ],
          ),
        ),
      ],
    );
  }

  // 构建媒体文件列表
  Widget _buildServicesList() {
    final screenSize = MediaQuery.of(context).size;

    return Container(
      padding: const EdgeInsets.all(14),
      constraints: BoxConstraints(
        maxWidth: screenSize.width * 0.2,
        maxHeight: screenSize.height * 0.9,
      ),
      decoration: BoxDecoration(
        color: Colors.white.withAlpha(40),
        borderRadius: BorderRadius.circular(20),
        border: Border.all(
            color: AppConstants.primaryColor.withAlpha(70), width: 1.5),
        boxShadow: [
          BoxShadow(
            color: Colors.black.withAlpha(30),
            blurRadius: 10,
            offset: const Offset(0, 3),
          ),
        ],
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        mainAxisSize: MainAxisSize.min,
        children: [
          // 媒体文件标题
          Row(
            children: [
              Icon(
                Icons.video_library_rounded,
                color: AppConstants.accentColor,
                size: 22,
              ),
              const SizedBox(width: 8),
              Expanded(
                child: Text(
                  '增值服务',
                  style: TextStyle(
                    fontSize: 16,
                    fontWeight: FontWeight.bold,
                    color: AppConstants.textColor,
                  ),
                  maxLines: 1,
                  overflow: TextOverflow.ellipsis,
                ),
              ),
              Container(
                padding: const EdgeInsets.symmetric(
                  horizontal: 8,
                  vertical: 2,
                ),
                decoration: BoxDecoration(
                  color: AppConstants.primaryColor.withAlpha(40),
                  borderRadius: BorderRadius.circular(12),
                  border: Border.all(
                    color: AppConstants.primaryColor.withAlpha(100),
                    width: 1,
                  ),
                ),
                child: Text(
                  '${_services.length}个',
                  style: TextStyle(
                    fontSize: 12,
                    fontWeight: FontWeight.w500,
                    color: AppConstants.primaryColor,
                  ),
                ),
              ),
            ],
          ),
          Divider(
            height: 16,
            color: Colors.white.withAlpha(70),
            thickness: 1,
          ),

          // 媒体文件列表
          if (_services.isEmpty)
            Expanded(
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Icon(
                    Icons.video_library_outlined,
                    size: 32,
                    color: AppConstants.textColor.withAlpha(30),
                  ),
                  const SizedBox(height: 8),
                  Text(
                    '暂无',
                    style: TextStyle(
                      fontSize: 14,
                      color: AppConstants.textColor,
                    ),
                  ),
                  const SizedBox(height: 6),
                  Text(
                    '暂无增值服务',
                    style: TextStyle(
                      fontSize: 12,
                      color: AppConstants.textColor.withAlpha(60),
                    ),
                    textAlign: TextAlign.center,
                  ),
                ],
              ),
            )
          else
            Expanded(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  // 媒体列表
                  Expanded(
                    child: ListView.builder(
                      itemCount: _services.length,
                      padding: EdgeInsets.zero,
                      itemBuilder: (context, index) {
                        final service = _services[index];
                        return _buildServiceItem(service);
                      },
                    ),
                  ),
                ],
              ),
            ),
        ],
      ),
    );
  }

  // 构建媒体文件项
  Widget _buildServiceItem(HotelServices service) {
    return GestureDetector(
      child: Container(
        margin: const EdgeInsets.only(bottom: 6),
        padding: const EdgeInsets.all(16),
        decoration: BoxDecoration(
          color: Colors.white.withAlpha(26),
          borderRadius: BorderRadius.circular(10),
          border: Border.all(
            color: AppConstants.secondaryColor.withAlpha(30),
            width: 1,
          ),
        ),
        child: Row(
          children: [
            // 媒体图标
            Container(
              padding: const EdgeInsets.all(6),
              decoration: BoxDecoration(
                color: Colors.blue.withAlpha(26),
                borderRadius: BorderRadius.circular(8),
              ),
              child: Icon(
                Icons.image_rounded,
                color: Colors.blue,
                size: 16,
              ),
            ),
            const SizedBox(width: 8),
            // 媒体名称和描述
            Expanded(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                mainAxisSize: MainAxisSize.min,
                children: [
                  Text(
                    service.name,
                    style: TextStyle(
                      fontSize: 16,
                      fontWeight: FontWeight.w600,
                      color: AppConstants.textColor,
                    ),
                    maxLines: 1,
                    overflow: TextOverflow.ellipsis,
                  ),
                  const SizedBox(height: 2),
                  Text(
                    service.price.toString(),
                    style: TextStyle(
                      fontSize: 18,
                      color: Colors.red,
                    ),
                    maxLines: 1,
                    overflow: TextOverflow.ellipsis,
                  ),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }
}
